#include "postitemwidget.h"
#include "ui_postitemwidget.h"

PostItemWidget::PostItemWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::PostItemWidget)
{
    ui->setupUi(this);

    // 主布局改为水平排列
    QHBoxLayout *mainLayout = new QHBoxLayout(this);
    mainLayout->setSpacing(15); // 图片与文字的间距
    mainLayout->setContentsMargins(15, 15, 15, 15); // 增加内边距

    // ====== 左侧图片区域 ======
    imageLabel = new QLabel;
    imageLabel->setFixedSize(180, 180); // 增大图片显示区域
    imageLabel->setAlignment(Qt::AlignCenter);
    imageLabel->setStyleSheet(
        "border: 1px solid #eeeeee;"
        "background: #f8f9fa;"
        "border-radius: 6px;"
    );
    imageLabel->setText("(无图片)");
    mainLayout->addWidget(imageLabel, 0, Qt::AlignTop); // 顶部对齐

    // ====== 右侧内容区域 ======
    QVBoxLayout *rightLayout = new QVBoxLayout;
    rightLayout->setSpacing(10);

    // 顶部作者信息（保持原样）
    QWidget *header = new QWidget;
    QHBoxLayout *headerLayout = new QHBoxLayout(header);
    headerLayout->setContentsMargins(0, 0, 0, 0);

    authorLabel = new QLabel("农场主: 张三");
    authorLabel->setStyleSheet("font-weight: bold; color: #2c7fb8; font-size: 14px;");
    timeLabel = new QLabel(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm"));
    timeLabel->setStyleSheet("color: #999999; font-size: 12px;");

    headerLayout->addWidget(authorLabel);
    headerLayout->addStretch();
    headerLayout->addWidget(timeLabel);
    rightLayout->addWidget(header);

    // 内容区域（支持滚动）
    contentLabel = new QLabel;
    contentLabel->setWordWrap(true);
    contentLabel->setStyleSheet(
        "margin: 0;"
        "font-size: 14px;"
        "line-height: 1.5;"
        "max-height: 80px;" // 限制高度避免过长
    );
    rightLayout->addWidget(contentLabel, 1); // 允许伸缩

    // 底部作物信息
    QWidget *footer = new QWidget;
    QHBoxLayout *footerLayout = new QHBoxLayout(footer);
    footerLayout->setContentsMargins(0, 0, 0, 0);

    plantLabel = new QLabel("作物: 番茄");
    plantLabel->setStyleSheet("color: #27ae60; font-size: 13px;");
    priceLabel = new QLabel("出价: ¥98.00");
    priceLabel->setStyleSheet("color: #e74c3c; font-weight: bold; font-size: 14px;");

    footerLayout->addWidget(plantLabel);
    footerLayout->addStretch();
    footerLayout->addWidget(priceLabel);
    rightLayout->addWidget(footer);

    mainLayout->addLayout(rightLayout, 1); // 右侧区域可伸缩

}

void PostItemWidget::setPostData(const QString &author, const QString &titleContent,
                 const QString &plantType, float price, const QString &imagePath)
{
    // 拆分标题和内容
    int splitPos = titleContent.indexOf('\n');
    if (splitPos != -1) {
        this->title = titleContent.left(splitPos);
        this->content = titleContent.mid(splitPos + 1);
    } else {
        this->title = titleContent;
        this->content = "";
    }

    this->author = author;
    this->plantType = plantType;
    this->price = price;
    this->imagePath = imagePath;

    // 更新UI显示
    authorLabel->setText("农场主: " + author);
    contentLabel->setText(this->content);
    plantLabel->setText("作物: " + plantType);
    priceLabel->setText(QString("出价: ¥%1").arg(price, 0, 'f', 2));

    // ====== 新增图片处理逻辑 ======
    if (!imagePath.isEmpty()) {
        QPixmap pixmap(imagePath);
        if (!pixmap.isNull()) {
            // 高质量缩放适应120x120区域
            pixmap = pixmap.scaled(imageLabel->width(), imageLabel->height(),
                                  Qt::KeepAspectRatio, Qt::SmoothTransformation);
            imageLabel->setPixmap(pixmap);
            imageLabel->setText("");  // 清除文本提示
        } else {
            imageLabel->setText("(图片加载失败)");
        }
    } else {
        imageLabel->setText("(无图片)");
    }

}

PostItemWidget::~PostItemWidget()
{
    delete ui;
}
